package com.xsm.ppsecret.presenter;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xsm.ppsecret.sql.DBHelper;
import com.xsm.ppsecret.sql.PPUserSql;
import com.xsm.ppsecret.ui.implview.IAddLogActivity;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-29.
 */

public class AddLogPresenter extends BasePresenter {

    private IAddLogActivity mAddLogActivity;

    public AddLogPresenter(IAddLogActivity addLogActivity) {
        mAddLogActivity = addLogActivity;
    }

    public void save(Context context, String str, int id) {
        if (str == null || str.isEmpty()) {
            mAddLogActivity.saveError("内容为空，不予保存");
            return;
        }
        DBHelper dbHelper = new DBHelper(context);
        SQLiteDatabase database = dbHelper.getWritableDatabase();
        boolean isOk;
        if (id == -1) {
            if (PPUserSql.saveLogEntry(database, str)) {
                mAddLogActivity.saveOk();
            } else {
                mAddLogActivity.saveError("保存失败，请重新尝试保存");
            }
        } else {
            int rows = PPUserSql.updateLogEntry(database, str, id);
            mAddLogActivity.saveOk();
        }
    }

}
